package com.tshopping.service.impl;

import com.tshopping.bean.*;
import com.tshopping.dao.*;
import com.tshopping.service.OrderService;
import com.tshopping.util.ResponseData;
import com.tshopping.util.TimeProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Time;
import java.text.ParseException;
import java.util.ArrayList;

/**
 * @name: OrderServiceImpl
 * @description: 订单业务层类
 * @author: Jackeys
 * @date: 2021-01-30 15:48
 */
@Service(value = "orderService")
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderDetailsDao orderDetailsDao;

    @Autowired
    private ParameterDao parameterDao;

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private StoreDao storeDao;
    @Override
    /**
     * @description 处理用户下订单
     * @Param: dataModel
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 16:15
     */
    public ResponseData addOrder(DataModel dataModel) throws ParseException {
        ResponseData responseData = ResponseData.ok();
        Order order=dataModel.getOrder();
        order.setTime(TimeProvider.getTime());
        ArrayList<OrderDetails>  orderDetails = dataModel.getOrderDetailsList();
        if(!orderDao.insertOrderByAllOrderData(order)){
            responseData.putDataValue("flag", false);
            return responseData;
        }
        for(OrderDetails orderDetails1:orderDetails){
            orderDetails1.setTime(TimeProvider.getTime());
            orderDetails1.setOrders(order.getId());
            if(!orderDetailsDao.insertOrderDetailsByAllData(orderDetails1)){
                responseData.putDataValue("flag", false);
                return responseData;
            }
        }
        responseData.putDataValue("flag", true);
        return responseData;
    }

    @Override
    /**
     * @description 处理用户完成订单
     * @Param: isFinish
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 16:29
     */
    public ResponseData updateIsFinishById(int isFinish, int id) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", orderDao.updateIsFinishById(isFinish, id));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户完成支付
     * @Param: isPay
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 16:29
     */
    public ResponseData updateIsPayById(Order order) {
        ResponseData responseData = ResponseData.ok();
        if(!orderDao.updateIsPayById(1,order.getId())){
            responseData.putDataValue("flag",false);
            return responseData;
        }
        if(!userDao.payAccountById(order.getTotal(),order.getUser())){
            responseData.putDataValue("flag",false);
            return responseData;
        }
        responseData.putDataValue("flag",true);
        return responseData;
    }

    @Override
    /**
     * @description 处理用户发货
     * @Param: isSendOut
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 16:29
     */
    public ResponseData updateIsSendOutById(Order order) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag",orderDao.updateIsSendOutById(1,order.getId()));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户收货
     * @Param: isReceive
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 16:29
     */
    public ResponseData updateIsReceiveById(Order order) {
        ResponseData responseData = ResponseData.ok();
        if(!orderDao.updateIsReceiveById(1,order.getId())){
            responseData.putDataValue("flag", false);
            return responseData;
        }
        if(!storeDao.addStoreSaleRoomAndSalesVolume(orderDetailsDao.selectCountByOrders(order.getId()),order.getTotal(),order.getStore())){
            responseData.putDataValue("flag", false);
            return responseData;
        }
        if(!orderDao.updateIsFinishById(1,order.getId())){
            responseData.putDataValue("flag", false);
            return responseData;
        }
        responseData.putDataValue("flag", true);
        return responseData;
    }

    @Override
    /**
     * @description 处理用户退款
     * @Param: isRefund
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 16:30
     */
    public ResponseData updateIsRefundById(Order order) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", orderDao.updateIsRefundById(1, order.getId()));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户完成退款
     * @Param: isFinishRefund
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 16:30
     */
    public ResponseData updateIsFinishRefundById(Order order) {
        ResponseData responseData = ResponseData.ok();
        if(!orderDao.updateIsFinishRefundById(1,order.getId())){
            responseData.putDataValue("flag", false);
            return responseData;
        }
        if(!orderDao.updateIsFinishById(1,order.getId())){
            responseData.putDataValue("flag", false);
            return responseData;
        }
        if(!userDao.addAccountById(order.getTotal(),order.getId())){
            responseData.putDataValue("flag", false);
            return responseData;
        }
        if(!storeDao.backStoreSaleRoomAndSalesVolume(orderDetailsDao.selectCountByOrders(order.getId()),order.getTotal(),order.getStore())){
            responseData.putDataValue("flag", false);
            return responseData;
        }
        responseData.putDataValue("flag", true);
        return responseData;
    }

    @Override
    /**
     * @description 处理用户评论
     * @Param: isCommented
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 16:30
     */
    public ResponseData updateIsCommentedById(Order order) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", orderDao.updateIsCommentedById(1,order.getId()));
        return responseData;
    }

    @Override
    /**
     * @description 处理用户获取订单
     * @Param: user
     * @Param: currentPage
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 16:31
     */
    public ResponseData selectOrdersByUser(int user, int currentPage) {
        ResponseData responseData = ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 5;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = orderDao.selectOrderCountByUser(user);
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        ArrayList<Order> orders = orderDao.selectOrderByUserID(user, start, size);
        responseData.putDataValue("page", page);
        responseData.putDataValue("orderList", orders);
        return responseData;
    }

    @Override
    /**
     * @description 处理商家处理订单
     * @Param: store
     * @Param: currentPage
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 16:31
     */
    public ResponseData selectOrderByStore(int store, int currentPage) {
        ResponseData responseData = ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 5;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = orderDao.selectOrderCountByStore(store);
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        ArrayList<Order> orders = orderDao.selectOrderByStoreId(store, start, size);
        responseData.putDataValue("page", page);
        responseData.putDataValue("orderList", orders);
        return responseData;
    }

    @Override
    /**
     * @description 更新订单地址
     * @Param: address
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 21:06
     */
    public ResponseData updateAddress(int address, int id) {
        ResponseData responseData=ResponseData.ok();
        responseData.putDataValue("flag",orderDao.updateOrderAddressById(address,id));
        return responseData;
    }

    @Override
    /**
     * @description 判断订单是否存在
     * @Param: user
     * @Param: goods
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 21:14
     */
    public ResponseData isOrderExist(int user, int goods) {
        ResponseData responseData=ResponseData.ok();
        boolean flag;
        if(orderDao.selectOrderCountByUserAndGoods(user,goods)==1){
            flag=true;
        }else{
            flag=false;
        }
        responseData.putDataValue("flag",flag);
        return responseData;
    }

    @Override
    /**
     * @description 保存订单
     * @Param: dataModel
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-31 08:41
     */
    public ResponseData record(DataModel dataModel) {
        ResponseData responseData=ResponseData.ok();
        ArrayList<Order> orders=dataModel.getOrders();
        for(Order order:orders){
            if(!orderDao.updateOrderAddressById(order.getAddress(),order.getId())){
                responseData.putDataValue("flag",false);
                return responseData;
            }

        }
        responseData.putDataValue("flag",true);
        return responseData;
    }

    @Override
    /**
     * @description 返回订单全部信息
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-31 14:18
     */
    public ResponseData selectOrdersById(int id) {
        ResponseData responseData=ResponseData.ok();
        DataModel dataModel=new DataModel();
        ArrayList<DataModel> dataModels=new ArrayList<>();
        ArrayList<Goods> goods=new ArrayList<>();
        ArrayList<Integer> counts=new ArrayList<>();
        ArrayList<Parameter> parameters=new ArrayList<>();
        Order order=orderDao.selectOrderById(id);
        ArrayList<OrderDetails> orderDetails=orderDetailsDao.selectOderDetailsByOrderId(id);
        for(OrderDetails orderDetails1:orderDetails){
            goods.add(goodsDao.selectGoodsByGoodsId(orderDetails1.getGoods()));
            counts.add(orderDetails1.getNumber());
            parameters.add(parameterDao.selectParameterById(orderDetails1.getParameter()));
        }
        dataModel.setOrder(order);
        dataModels.add(dataModel);
        return responseData;
    }

}
